package approve.action;

import java.util.List;
import java.util.Timer;

import project.active.LogoutActivity;

import nguyen.dao.ReceiverDocumentDAO;
import resource.pojo.*;
import model.pojo.Document;
import model.pojo.Employee;
import android.app.Activity;
import android.app.NotificationManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import approval.dao.ApproveDocumentDAO;
import approval.dao.ApprovedocDAO;

public class My_App_DocumentSending extends Activity {
	String database = "admin";
	ApproveDocumentDAO appDD = new ApproveDocumentDAO();

	public String getDatabase() {
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			database = extras.getString("database");
		}
		return database;
	}

	public String getLang() {
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			lang = extras.getString("lang");
		}
		return lang;
	}

	String lang = "EN";
	Approvedoc AppDoc = null;
	Employee emp = null;
	ListView list;

	Timer timer = new Timer();
	NotificationManager mNotificationManager = null;
	private int SIMPLE_NOTFICATION_ID;
	TextView logout, username;
	Bundle language;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		// System.out.println("sdggd");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_app_documentsendding);
		getDatabase();
		emp = (Employee) getIntent().getExtras().get("em");
		language = getIntent().getExtras().getBundle(getLang());

		mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		logout = (TextView) findViewById(R.id.logout);
		username = (TextView) findViewById(R.id.username);

		logout.setOnClickListener(listenerLogout);
		username.setText(emp.getUserName());

		// System.out.println("emp:"+emp.getIdemployee());
		list = (ListView) findViewById(R.id.lst_mydocumentsending);
		list.setAdapter(new ArrayAdapter<Approvedoc>(this,
				android.R.layout.simple_list_item_1, getListDocumentSending()));
		list.setOnItemClickListener(viewForm);

	}

	private OnClickListener listenerLogout = new OnClickListener() {

		public void onClick(View v) {
			// TODO Auto-generated method stub
			// Intent i=new Intent(MainFrame.this, LoginActivity.class);
			timer.cancel();
			mNotificationManager.cancel(SIMPLE_NOTFICATION_ID);

			Intent i = new Intent(My_App_DocumentSending.this,
					LogoutActivity.class);
			i.putExtra("database", getDatabase());
			startActivity(i);
		}
	};
	private OnItemClickListener viewForm = new OnItemClickListener() {
		public void onItemClick(android.widget.AdapterView<?> lstAppDoc,
				android.view.View arg1, int arg2, long arg3) {
			Approvedoc AppDoc = (Approvedoc) lstAppDoc.getItemAtPosition(arg2);
			if (AppDoc.getiDTypeDoc() == 16) {
				Intent intent = new Intent(My_App_DocumentSending.this,
						My_viewform16.class);
				intent.putExtra("AppDoc", AppDoc);
				intent.putExtras(getIntent().getExtras());
				startActivity(intent);
			} else if (AppDoc.getiDTypeDoc() == 15) {
				Intent intent = new Intent(My_App_DocumentSending.this,
						My_ViewForm15.class);
				intent.putExtra("AppDoc", AppDoc);
				intent.putExtras(getIntent().getExtras());
				startActivity(intent);
			} else if (AppDoc.getiDTypeDoc() < 11) {
				Intent intent = new Intent(My_App_DocumentSending.this,
						My_ViewForm_Lessthan10.class);
				intent.putExtra("AppDoc", AppDoc);
				intent.putExtras(getIntent().getExtras());
				startActivity(intent);
			} else if (AppDoc.getiDTypeDoc() == 11) {
				Intent intent = new Intent(My_App_DocumentSending.this,
						My_ViewForm11.class);
				intent.putExtra("AppDoc", AppDoc);
				intent.putExtras(getIntent().getExtras());
				startActivity(intent);
			} else if (AppDoc.getiDTypeDoc() == 12) {
				Intent intent = new Intent(My_App_DocumentSending.this,
						My_ViewForm12.class);
				intent.putExtra("AppDoc", AppDoc);
				intent.putExtras(getIntent().getExtras());
				startActivity(intent);
			} else if (AppDoc.getiDTypeDoc() == 13) {
				Intent intent = new Intent(My_App_DocumentSending.this,
						My_ViewForm13.class);
				intent.putExtra("AppDoc", AppDoc);
				intent.putExtras(getIntent().getExtras());
				startActivity(intent);
			} else {
				Intent intent = new Intent(My_App_DocumentSending.this,
						My_ViewForm14.class);
				intent.putExtra("AppDoc", AppDoc);
				intent.putExtras(getIntent().getExtras());
				startActivity(intent);
			}

		};
	};

	public List<Approvedoc> getListDocumentSending() {
		// System.out.println("aaaaa");
		// System.out.println("lst1"+ApproveDocumentDAO.getAllDoc(2,"","",emp.getIdemployee(),
		// getDatabase()));
		List<Approvedoc> lst = ApproveDocumentDAO.getAllDoc(2, "", "",
				emp.getIdemployee(), getDatabase());
		if (lst.size() == 0) {
			// System.out.println(list.toString() + "Khong co du lieu");
			TextView textView = new TextView(this);
			textView.setText(language.getString("nodata"));
			setContentView(textView);
		} else {
			return lst;
		}
		return lst;

	}

}
